package main

import (
	item "cloudWego/kitex_gen/example/shop/item/itemservice"
	"github.com/cloudwego/kitex/pkg/rpcinfo"
	"github.com/cloudwego/kitex/server"
	"github.com/kitex-contrib/registry-nacos/v2/registry"
	"github.com/nacos-group/nacos-sdk-go/v2/clients"
	"github.com/nacos-group/nacos-sdk-go/v2/common/constant"
	"github.com/nacos-group/nacos-sdk-go/v2/vo"
	"log"
	"net"
)

func main() {
	//svr := item.NewServer(new(ItemServiceImpl))
	//
	//err := svr.Run()
	//
	//if err != nil {
	//	log.Println(err.Error())
	//}

	sc := []constant.ServerConfig{
		*constant.NewServerConfig("192.168.8.140", 8848),
	}

	cc := constant.ClientConfig{
		NamespaceId:         "go-zero1",
		TimeoutMs:           5000,
		NotLoadCacheAtStart: true,
		LogDir:              "tmp/nacos/log",
		CacheDir:            "tmp/nacos/cache",
		LogLevel:            "info",
		Username:            "nacos",
		Password:            "nacos",
	}

	cli, err := clients.NewNamingClient(
		vo.NacosClientParam{
			ClientConfig:  &cc,
			ServerConfigs: sc,
		},
	)
	if err != nil {
		panic(err)
	}

	addr, _ := net.ResolveTCPAddr("tcp", "0.0.0.0:8080")

	// 作为客户端去请求 stock 服务
	itemServiceImpl := new(ItemServiceImpl)
	stockCli, err := NewStockClient()
	if err != nil {
		log.Fatal(err)
	}
	itemServiceImpl.stockCli = stockCli

	//注册 item 服务
	//svr := item.NewServer(new(ItemServiceImpl),
	svr := item.NewServer(itemServiceImpl,
		server.WithServerBasicInfo(&rpcinfo.EndpointBasicInfo{ServiceName: "item"}),
		server.WithServiceAddr(addr),
		server.WithRegistry(registry.NewNacosRegistry(cli)),
	)
	if err := svr.Run(); err != nil {
		log.Println("server stopped with error:", err)
	} else {
		log.Println("server stopped")
	}

	//svr := item.NewServer(itemServiceImpl)

	err = svr.Run()

	if err != nil {
		log.Println(err.Error())
	}

}
